<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  props: ["userId"],
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  data() {
    return {
      appWindow: window,
      apiUrl: {
        get: "mall-site-portal/member/getRelationChainList",
        updateRelation: "mall-site-portal/member/modifyRelationChain",
        detail: "store/member/detail", //mall-site-portal/member/getItem
      },

      usersIds: [],
      isloading: false,

      has: false, //是否有上级

      detailInfo: {}, //会员信息
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      // this.getRelation();
      this.getUserInfo();
    },

    // 获取上级关系链
    async getRelation() {
      this.isloading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.get,
          data: {
            memberId: this.userId,
            levelType: 1,
            level: 1,
          },
        },
        window
      );

      this.isloading = false;

      if (res._ok) {
        console.log(res);
        // 有上级
        if (!this.$vFN.Utils.isEmpty(res.data[0].referrerId)) {
          this.has = true;
          this.usersIds.push(res.data[0].referrerId);

          // this.getUserInfo(res.data[0].referrerId);
        }
        // 无上级
        else {
          this.has = false;
        }
      }
    },

    // 获取上级关系链的信息
    async getUserInfo() {
      this.isloading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
          data: {
            id: this.userId,
          },
        },
        window
      );

      this.isloading = false;
      if (res._ok) {
        this.detailInfo = res.data.referrerMember;
      }
    },

    // 修改关系链弹窗  获取想更改的用户的id
    changeRelation() {
      this.$vFN.Directives.SelectUser({
        appWindow: this.appWindow,
        usersIds: this.usersIds,
        count: 1,
        onSuccess: (res) => {
          // 返回客户数组  因为只选一个 所以res[0]
          console.log(res, "res");
          this.updateRelation(res[0]);
        },
      });
    },

    // 发送修改关系链请求
    async updateRelation(referrerId) {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.updateRelation,
          data: {
            memberId: this.userId,
            referrerId: referrerId,
          },
        },
        window
      );

      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.getRelation();
      }
    },

    //会员渠道转文字
    // channelType2Text(str_) {
    //   let text = "";
    //   switch (str_) {
    //     case "JSAPI":
    //       text = "公众号";
    //       break;
    //     case "MINIAPP":
    //       text = "小程序";
    //       break;
    //     case "H5":
    //       text = "H5";
    //       break;
    //   }
    //   return text;
    // },
  },
  filters: {
    //   过滤性别
    getSex(num) {
      if (num == 0) {
        return "未知";
      } else if (num == 1) {
        return "男";
      } else if (num == 2) {
        return "女";
      } else {
        return "暂无";
      }
    },
  },
};
</script>
